{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tensorflow2.0 小练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实现softmax函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def softmax(x):\n",
    "    ##########\n",
    "    '''实现softmax函数，只要求对最后一维归一化，\n",
    "    不允许用tf自带的softmax函数'''\n",
    "    ##########\n",
    "    return prob_x\n",
    "\n",
    "test_data = np.random.normal(size=[10, 5])\n",
    "(softmax(test_data).numpy() - tf.nn.softmax(test_data, axis=-1).numpy())**2 <0.0001"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实现sigmoid函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(x):\n",
    "    ##########\n",
    "    '''实现sigmoid函数， 不允许用tf自带的sigmoid函数'''\n",
    "    ##########\n",
    "    return prob_x\n",
    "\n",
    "test_data = np.random.normal(size=[10, 5])\n",
    "(sigmoid(test_data).numpy() - tf.nn.sigmoid(test_data).numpy())**2 < 0.0001"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实现 softmax 交叉熵loss函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def softmax_ce(x, label):\n",
    "    ##########\n",
    "    '''实现 softmax 交叉熵loss函数， 不允许用tf自带的softmax_cross_entropy函数'''\n",
    "    ##########\n",
    "    return loss\n",
    "\n",
    "test_data = np.random.normal(size=[10, 5])\n",
    "prob = tf.nn.softmax(test_data)\n",
    "label = np.zeros_like(test_data)\n",
    "label[np.arange(10), np.random.randint(0, 5, size=10)]=1.\n",
    "\n",
    "((tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(label, test_data))\n",
    "  - softmax_ce(prob, label))**2 < 0.0001).numpy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实现 sigmoid 交叉熵loss函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 1. 1. 0. 1. 1. 1. 0.]\n"
     ]
    }
   ],
   "source": [
    "def sigmoid_ce(x, label):\n",
    "    ##########\n",
    "    '''实现 softmax 交叉熵loss函数， 不允许用tf自带的softmax_cross_entropy函数'''\n",
    "    ##########\n",
    "    return loss\n",
    "\n",
    "test_data = np.random.normal(size=[10])\n",
    "prob = tf.nn.sigmoid(test_data)\n",
    "label = np.random.randint(0, 2, 10).astype(test_data.dtype)\n",
    "print (label)\n",
    "\n",
    "((tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(label, test_data))- sigmoid_ce(prob, label))**2 < 0.0001).numpy()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
